
var doubleClickFlag = false; // doubleClick Flag
var timeoutId = null; // setTimeout's ID

function clickHandler(event) {
	
	// Don't call clickLogicFunction twice when doubleClick
	if (timeoutId == null) {
		timeoutId = setTimeout(function() {clickLogicFunction(event)}, 300);
	}
}

function dbclickHandler() {
	
	doubleClickFlag = true;
}

function clickLogicFunction(event) {
	
	timeoutId = null;
	if (doubleClickFlag) {
		doubleClickFlag = false;
		openColorSettingPanel(event);
	} else {
		selectColors(event);
	}
}

/**
 * Click handler, change the current color setting with the selected option.
 */
function selectColors(event) {
	
	var targetLbl = event.target;
	var num = targetLbl.getAttribute("value");
	window.opener.chooseColorOptionHandler(num - 1);
	
	// Close the dialog
	var dialog = document.getElementById("qcs_color_dialog");
	dialog.acceptDialog();
}

/**
 * Open the custom color Dialog.
 */
function openColorSettingPanel(event) {
	
	var targetLbl = event.target;
	var num = targetLbl.getAttribute("value") - 1;
	var colorItem = colorArr[num];
	window.openDialog("chrome://quickcolorswitcher/content/xul/colorPref.xul", 
						"colorPref", "centerscreen,modal", colorItem, num);
}

/**
 * Change the selected color setting's value.
 */
function changeColorSetting(item, index) {
	
	if (!equals(item, colorArr[index])) {
		var targetGrid = document.getElementById("colorPanelGrid");
		var lbls = targetGrid.getElementsByTagNameNS(XUL_NS, "label");
		for (var i = 0; i < lbls.length; ++i) {
			var lbl = lbls[i];
			if (lbl.getAttribute("value") == (index + 1)) {
				lbl.style.backgroundColor = item.background;
				lbl.style.color = item.foreground;
			}
		}
		colorArr[index] = item;
		if (window.opener.selectedIndex == index) {
			window.opener.changeIconStyleToSelected();
			window.opener.setColorToSelectedOption();
		} else {
			window.opener.setSelectedIndex(index);
			window.opener.setColorToSelectedOption();
		}
		window.opener.saveSettings();
	}
}

/**
 * Compares the two specified objects.
 * The result is true if and only if the two specified objects are not null and
 * all of colorObj1's user-defined properties's value equal colorObj2's.
 */
function equals(colorObj1, colorObj2) {
	
	if ((colorObj1 == null) || (colorObj2 == null)) return false;
	for (var name in colorObj1) {
		if (colorObj1[name] != colorObj2[name]) return false; 
	}
	return true;
}

/**
 * Generate the color options when loading completed.
 * You cann't retrieve element in the xul file before loading completed.
 */
function ColorDailogInit() {

	window.addEventListener ("load", initColorDialog, false);
}

const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";

/**
 * Generate the color options.
 */
function initColorDialog() {

	var tipText = strBundle.getString("color.options.tooltip");
	var targetGrid = document.getElementById("colorPanelGrid");
	var len = colorArr.length;
	var sqrtNum = Math.ceil(Math.sqrt(len));
	
	// append columns node
	var columns = document.createElementNS(XUL_NS, "columns");
	for (var i = 0; i < sqrtNum; ++i) {
		var column = document.createElementNS(XUL_NS, "column");
		columns.appendChild(column);
	}
	targetGrid.appendChild(columns);
	
	// append rows node
	var rows = document.createElementNS(XUL_NS, "rows");
	var row = null;
	for (var i = 0; i < len; ++i) {
		if (i % sqrtNum == 0) {
			row = document.createElementNS(XUL_NS, "row");
			rows.appendChild(row);
		}
		var lbl = document.createElementNS(XUL_NS, "label");
		lbl.setAttribute("value", i + 1);
		lbl.setAttribute("class", "lblInBigBox");
		lbl.setAttribute("tooltiptext", tipText);
		// initialize colors
		lbl.style.backgroundColor = colorArr[i].background;
		lbl.style.color = colorArr[i].foreground;
		// add event listener
		lbl.addEventListener("click", clickHandler, false);
		lbl.addEventListener("dblclick", dbclickHandler, false);
		row.appendChild(lbl);
	}
	targetGrid.appendChild(rows);
}

var colorArr = window.arguments[0]; // note:by reference
var strBundle = window.arguments[1];
ColorDailogInit();
